import { onMounted, ref, toRef } from "vue";
import CustomTable from "@/components/CustomTable";
import PublicTable from "@/components/PublicTable";
import PublicDialog from "@/components/PublicDialog.vue";
import {
  ElMessage,
  ElMessageBox,
  ElButton,
  ElUpload,
  ElInput,
} from "element-plus";
import { lingchuangPointExport } from "@/api/dailyPaper/lingchuang";
import { exportToExcel } from "@/hooks/useCommon";

export default (props = {}) => {
  const {
    getFn = () => {},
    updateFn = () => {},
    importFn = () => {},
    displayFn = () => {},
  } = props;

  const dataSource = ref([]);
  const backData = ref([]);
  const showImportModal = ref(false);
  const fileList = ref([]);
  const isEdit = ref(false);

  const handleUploadFile = async (file) => {
    let uploadFile = new FormData();
    uploadFile.append("file", file.file);
    const { data, code } = await importFn(uploadFile);
    if (code == "200") {
      ElMessage.success("上传成功");
      fileList.value = [];
      getSummaryByBatchNo(data);
    }
  };
  /**导入回显 */
  const getSummaryByBatchNo = async (batchNo) => {
    const { data, code } = await displayFn({ batchNo });
    if (code != "200") return;
    backData.value = [...data];
    showImportModal.value = true;
    getList();
  };

  /**合计行 */
  const getSummaries = (params) => {
    const { columns, data } = params;
    const sums = [];
    columns.map((item, index) => {
      if (index === 0) {
        sums[index] = "重要事项补充说明";
        return;
      } else if (index > 6) {
        const prop = item.property;
        let str = "";
        data.map((item) => {
          const name = item.name;
          const memo = item.days[prop]?.memo;
          str += `${memo ? `${name}:${memo};` : ""}`;
        });
        sums[index] = str;
      } else {
        sums[index] = "-";
      }
    });
    return sums;
  };

  const getSummary = (param) => {
    const { columns, data } = param;
    const sums = [];
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = "合计";
        return;
      }
      const values = data.map((item) => Number(item[column.property]));
      if (!values.every((value) => Number.isNaN(value))) {
        sums[index] = `${values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!Number.isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0)}`;
      } else {
        sums[index] = "-";
      }
    });
    return sums;
  };

  /**获取列表 */
  const getList = async () => {
    const { data, code } = await getFn();
    if (code != "200") return;
    const tableData = data.map((item) => {
      const obj = {};
      for (const key in item.days) {
        const day = item.days[key];
        const num = day.num;
        obj[key] = num;
      }
      return {
        ...item,
        ...obj,
      };
    });

    dataSource.value = tableData;
  };

  const handleEdit = () => (isEdit.value = true);
  const handleSave = async () => {
    const { data, code } = await updateFn([...backData.value]);
    if (code === "200") {
      getList();
      isEdit.value = false;
      ElMessage.success("编辑成功");
      showImportModal.value = false;
      backData.value = [];
    }
  };

  onMounted(() => getList());
  return {
    dataSource,
    fileList,
    backData,
    showImportModal,
    isEdit,
    getList,
    handleEdit,
    handleSave,
    handleUploadFile,
    getSummary,
    CustomTable,
    PublicDialog,
    PublicTable,
  };
};

export function useImport(props) {
  const { importFn, onFinish = () => {} } = props;
  const fileList = ref([]);
  const handleUploadFile = async (file) => {
    let uploadFile = new FormData();
    uploadFile.append("file", file.file);
    const { data, code } = await importFn(uploadFile);
    fileList.value = [];
    if (code != "200") return;
    ElMessage.success("上传成功");
    onFinish();
  };
  return {
    fileList,
    handleUploadFile,
  };
}

export function useList(props) {
  const { getFn = () => {} } = props;
  const dataSource = ref([]);

  const getSummary = (param) => {
    const { columns, data } = param;
    const sums = [];
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = "合计";
        return;
      }
      const values = data.map((item) => Number(item[column.property]));
      if (!values.every((value) => Number.isNaN(value))) {
        sums[index] = `${values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!Number.isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0)}`;
      } else {
        sums[index] = "-";
      }
    });
    return sums;
  };

  /**获取列表 */
  const getList = async () => {
    const { data, code } = await getFn();
    if (code != "200") return;
    const tableData = data.map((item) => {
      const obj = {};
      for (const key in item.days) {
        const day = item.days[key];
        const num = day.num;
        obj[key] = num;
      }
      return {
        ...item,
        ...obj,
      };
    });

    dataSource.value = tableData;
  };

  onMounted(() => getList());
  return {
    dataSource,
    getSummary,
    getList,
    CustomTable,
  };
}

export function useExport({ fileName = "", exportFn, params = {} }) {
  const onExport = async () => {
    const res = await exportFn({ ...params });
    exportToExcel(res.data, fileName);
  };

  return {
    onExport,
  };
}
